package com.qdu.study_app.Activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.qdu.study_app.R;
import com.qdu.study_app.Receiver.TimeCountReceiver;
import com.qdu.study_app.bean.Clock;
import com.qdu.study_app.fragment.StudyFragment;
import com.qdu.study_app.service.ClockService;
import com.qdu.study_app.utils.MyToast;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class Activity_clock extends AppCompatActivity {
    private TextView timeCount;
    private TimeCountReceiver timeCountReceiver;
    private IntentFilter clockIntentFilter;
    private Intent activityIntent;
    private Intent serviceIntent;
    private Button stopTask;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock);
        initView();
        initService();
        initReceiver();
    }

    private void initView() {
        timeCount = findViewById(R.id.time_count_down);
        stopTask = findViewById(R.id.stop_task_btn);
        activityIntent = getIntent();
        stopTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Clock clock = new Clock();
                String objId = activityIntent.getStringExtra(StudyFragment.TASK_ID);
                //         System.out.println("..............."+activityIntent.getStringExtra(StudyFragment.TASK_TIME));
                int time = Integer.parseInt(activityIntent.getStringExtra(StudyFragment.TASK_TIME));
                int img_index = Integer.parseInt(activityIntent.getStringExtra(StudyFragment.TASK_IMG));
                if (ClockService.TASK_FINISHED_FLAG == 0) {
                    clock.setFinished(false);
                    clock.setTime(time);
                    clock.setImg_index(img_index);
                    clock.update(objId, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                System.out.println("任务未完成——————————");

                                finish();
                            }
                        }
                    });
                }else {
                    System.out.println("任务完成——————————");

                    finish();
                }

            }
        });

        //MyToast.showToast(Activity_clock.this,activityIntent.getStringExtra(StudyFragment.USER_ID), Toast.LENGTH_SHORT);
    }

    private void initService() {
        // 获取前一个Activity传递过来的Intent，里面包含着传递的数据
        activityIntent = getIntent();
        // 创建一个用于注册服务的Intent
        serviceIntent = new Intent(this, ClockService.class);
        serviceIntent.putExtra("time", activityIntent.getStringExtra("time"));
        serviceIntent.putExtra(StudyFragment.TASK_TIME, activityIntent.getStringExtra(StudyFragment.TASK_TIME));
        serviceIntent.putExtra(StudyFragment.USER_ID, activityIntent.getStringExtra(StudyFragment.USER_ID));

        // serviceIntent.putExtra(StudyFragment.USER_ID,activityIntent.getStringExtra(StudyFragment.USER_ID));
        // 启动服务
        startService(serviceIntent);
    }

    private void initReceiver() {
        // 创建一个广播Receiver
        timeCountReceiver = new TimeCountReceiver(timeCount);
        // 注册广播
        clockIntentFilter = new IntentFilter();
        clockIntentFilter.addAction(ClockService.ACTION_TIME_TICK);
        registerReceiver(timeCountReceiver, clockIntentFilter);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消注册的广播
        unregisterReceiver(timeCountReceiver);
        // 停止服务
        stopService(serviceIntent);
    }
    /*
     * 让Activity不销毁，直接进入后台
     * */

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            moveTaskToBack(true); // 让Activity在后台运行
            // 发送广播
            Intent backIntent = new Intent(ClockService.ACTION_TASK_TOBACK);
            sendBroadcast(backIntent);

            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
}
